package es.abredatos.energyes

import java.util.Date;

class DateService {
	
	/**
	 * This method returns the last year with production items associated.
	 */
	Integer findLastYearWithProductionItems() {
		return ProductionItem.createCriteria().get {
			projections {
				max('year')
			}
		}
	}
	
	/**
	 * This method returns the last month with production items for the
	 * passed year.
	 */
	Integer findLastMonthWithProductionItemsByYear(Integer year) {
		return ProductionItem.createCriteria().get {
			projections {
				max('month')
			}
			eq('year', year)
		}
	}
	
	/**
	* This method returns the name of the month associated with the passed
	* date, in spanish ('Enero', 'Mayo', etc.)
	*/
   String getMonthAsString(Date date) {
	   new java.text.SimpleDateFormat('MMMM', new Locale('es', 'ES')).format(date).capitalize()
   }
}